package com.jic.market.impl;

import com.google.common.base.Joiner;
import com.jic.market.common.constant.DbEffectConstant;
import com.jic.market.common.constant.PromotionMutexConstants;
import com.jic.market.common.enums.DeleteFlagEnum;
import com.jic.market.common.enums.MutexFlagEnum;
import com.jic.market.common.enums.TypeOfProductEnum;
import com.jic.market.common.util.LoginService;
import com.jic.market.entity.*;
import com.jic.market.enums.FrontPromTypeConditionTypeEnum;
import com.jic.market.mapper.*;
import com.jic.market.orm.Pagination;
import com.jic.market.request.*;
import com.jic.market.response.*;
import com.jic.market.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author ：xuyongliang
 * @date ：Created in 2020/1/16 10:02 AM
 * @description：促销商家商品服务
 * @modified By：`
 * @version: 1.0
 */
@Slf4j
@Service
public class PromotionComplexServiceImpl implements PromotionComplexService {

    @Resource
    PromotionEntityMapper promotionEntityMapper;

    @Resource
    PromotionRuleEntityMapper promotionRuleEntityMapper;

    /**
     * 组合活动-保存促销规则
     * @param request
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public PromotionResponse savePromotion(PromotionRequest request) {
        PromotionEntity promotionEntity = new PromotionEntity();
        BeanUtils.copyProperties(request, promotionEntity);
        promotionEntity.setPromType(6);
        promotionEntity.setProPlatform("2");
        promotionEntity.setProMemberLevel("3");
        promotionEntity.setProMemberType("0");
        promotionEntity.setChannelCodes(request.getChannelCodes());
        promotionEntity.setProUserType(Joiner.on("|").join(request.getProUserType()));
        promotionEntityMapper.insert(promotionEntity);

        PromotionResponse response = new PromotionResponse();
        BeanUtils.copyProperties(promotionEntity, response);
        List<PromotionRuleRequest.PromotionRuleListRequest> promotionRules = request.getPromotionRules();
        if (CollectionUtils.isNotEmpty(promotionRules)) {
            promotionRules.forEach(x -> {
                FrontPromTypeConditionTypeEnum frontPromTypeConditionTypeEnum = FrontPromTypeConditionTypeEnum.lookByFrontType(request.getFrontPromType());
                if (frontPromTypeConditionTypeEnum != null) {
                    //保存promotion_rule数据
                    PromotionRuleEntity ruleEntity = new PromotionRuleEntity();
                    ruleEntity.setPromotionId(promotionEntity.getId());
                    ruleEntity.setConditionType(frontPromTypeConditionTypeEnum.getConditionType());
                    ruleEntity.setConditionValue(x.getConditionValue() != null ? x.getConditionValue().multiply(frontPromTypeConditionTypeEnum.getContentValueMultiply()).longValue() : null);
                    ruleEntity.setContentValue(x.getContentValue() != null ? x.getContentValue().multiply(frontPromTypeConditionTypeEnum.getContentValueMultiply()) : null);
                    ruleEntity.setContentType(request.getFrontPromType());
                    promotionRuleEntityMapper.insert(ruleEntity);
                }
            });
        }
        return response;
    }
}
